<?php
namespace Modules\ShopAdmin\Http\Controllers;
use Illuminate\Http\Request;
use Modules\Admin\Http\Requests\CommonStatusRequest;
use Modules\Admin\Http\Requests\CommonSortRequest;
use Modules\Admin\Http\Requests\CommonDeleteRequest;
use Modules\ShopAdmin\Models\ShopProductType as ShopProductTypeModel;
use Modules\Admin\Models\Image as ImageModel;
class ProductTypeController extends CommonShopController
{
	/**
	 * @name  列表  
	 */
    public function index()
    { 
        return view('shopadmin::product_type.index');
    }
	/**
	 * @name  列表数据
	 */
    public function data(Request $request)
    {
        $arr = ShopProductTypeModel::with('image_one')->orderBy('pid','asc')->orderBy('sort','asc')->get();
		foreach($arr as $k=>$v){
			$arr[$k]['lay_is_open']=false;
			if($v['image_one']['status'] == 1){
				$arr[$k]['image'] = $v['image_one']['url'];
			}else{
				$arr[$k]['image'] = '';
			}
		}
		return response()->json(["code"=>0,"msg"=> config('admin.limit_msg'),"data"=>$arr,"is"=>true,"tip"=>"操作成功"]);		
    }
	/**
	 * @name  添加页面
	 */
    public function create()
    { 
		$auth_rule_info = (new ShopProductTypeModel)->lefts();
		return view('shopadmin::product_type.create',compact('auth_rule_info'));
    }
	/**
	 * @name  添加数据处理 
	 */
    public function store(Request $request)
    {	
		$data = $request->all();
		if($data['image_id']){
			$data = (new ImageModel())->add_image($data,'file');
			if(!is_array($data)){
				return $data;
			};
		}else{
			unset($data['file']);
		}
		$ShopProductTypeModel = new ShopProductTypeModel();
		if($data['pid'] == 0){
			$data['level'] = 1;
		}else{
			$data['level'] = $ShopProductTypeModel->where(['id'=>$data['pid']])->value('level') + 1;
		}
		return $ShopProductTypeModel->common_create($data,route('shop_admin.product_type.index'));  
    }
	/**
	 * @name  添加子级页面
	 */
    public function create_sublevel($id)
    {   
		$goods_type_info = ShopProductTypeModel::find($id);
		return view('shopadmin::product_type.create_sublevel',compact('goods_type_info'));
    }
	/**
	 * @name  添加子级数据处理
	 */
    public function store_sublevel(Request $request)
    {
		$data = $request->all();
		if($data['image_id']){
			$data = (new ImageModel())->add_image($data,'file');
			if(!is_array($data)){
				return $data;
			};
		}else{
			unset($data['file']);
		}
		$ShopProductTypeModel = new ShopProductTypeModel();
		$data['level'] = $ShopProductTypeModel->where(['id'=>$data['pid']])->value('level') + 1;		
		return $ShopProductTypeModel->common_create($data,route('shop_admin.product_type.index'));
    }
	/**
	 * @name  编辑页面
	 */
    public function edit($id)
    {
		$info = ShopProductTypeModel::with('image_one')->find($id);
		return view('shopadmin::product_type.edit',compact('info'));
    }
	/**
	 * @name  编辑数据处理
	 */
    public function update(Request $request,$id)
    {
		$data = $request->all();
		if($data['image_id']){
			$data = (new ImageModel())->edit_image('pictures',$id,$data,'file');
			if(!is_array($data)){
				return $data;
			};
		}else{
			unset($data['file']);
		}
		return (new ShopProductTypeModel())->common_update($data,$id,route('shop_admin.product_type.index')); 
    }
	/**
	 * @name  调整状态数据处理
	 */
    public function status(CommonStatusRequest $request,$id)
    {
		return (new ShopProductTypeModel())->common_status_update($request->all(),$id,route('shop_admin.product_type.index')); 
    }
	/**
	 * @name  调整状态数据处理
	 */
    public function open(Request $request,$id)
    {
		return (new ShopProductTypeModel())->common_status_update($request->all(),$id,route('shop_admin.product_type.index')); 
    }
	/**
	 * @name  排序数据处理
	 */
    public function sorts(CommonSortRequest $request,$id)
    {
		return (new ShopProductTypeModel())->common_status_update($request->all(),$id,route('shop_admin.product_type.index'));  
    }
	/**
	 * @name  删除数据处理
	 */
    public function destroy(CommonDeleteRequest $request)
    {	$id = (new ImageModel())->del_image('shop_product_types',$request->get('id'));
		if(!is_numeric($id)){
			return $id;
		};
		return (new ShopProductTypeModel())->column_delete($request->get('id'),route('shop_admin.product_type.index'));  
    }
	
	
	
	
	
	
}
